<template>
	<div id="deptPage">
		<shyt-table
			ref="deptSettingTable"
			:columns="columns"
			:data="tableData"
			:auto-resize="true"
			:keep-source="true"
			:pager-config="pageConfig"
			:toolbar-config="toolbarConfig"
			:edit-config="{ trigger: 'dbclick', mode: 'row', showStatus: true }"
			@page-change="pageChange"
		>
			<template #deptBelongingType="{ row }">
				<span>{{
					dictModelFun(deptBelongingTypeList, row.deptBelongingType)
				}}</span>
			</template>
			<template #deptType="{ row }">
				<span>{{ dictModelFun(deptTypeList, row.deptType) }}</span>
			</template>
			<template #isIcuFlag="{ row }">
				<span>{{ isFlagFun(row.isIcuFlag) }}</span>
			</template>
			<template #isCharacteristicDeptFlag="{ row }">
				<span>{{ isFlagFun(row.isCharacteristicDeptFlag) }}</span>
			</template>
			<template #whetherEnable="{ row }">
				<span>{{ dictModelFun(whetherEnableList, row.whetherEnable) }}</span>
			</template>
			<template #toolbar_buttons>
				<el-input
					v-model="orgNameOrInputCode"
					class="dept_input"
					placeholder="请输入科室代码/名称/拼音码"
					size="medium"
					clearable
				></el-input>
				<el-button type="primary" size="small" @click="handlerSeach">
					查询
				</el-button>
				<el-button type="primary" size="small" @click="handlerAddClick">
					新增
				</el-button>
			</template>
			<template #edit="{ row }">
				<el-button
					type="text"
					size="small"
					:datalist="row"
					@click="handlerEditRow(row)"
				>
					编辑
				</el-button>
				<el-button type="text" size="small" @click="deldectDept(row)">
					删除
				</el-button>
			</template>
		</shyt-table>
		<deptDialog
			ref="deptDialog"
			:visible.sync="dialogVisible"
			:title="dialogTitle"
			:dept-data.sync="deptData"
			:dept-dialog-type="deptDialogType"
			:dept-belonging-type-list="deptBelongingTypeList"
			:dept-type-list="deptTypeList"
			:whether-enable-list="whetherEnableList"
			:row-repetition="rowRepetition"
			@submit="handlerSubmit"
		/>
	</div>
</template>
<script>
import {
	getOrgList,
	orgInsert,
	updateOrg,
	deleteOrg,
	getDeptPageList,
} from '@/http/org'
import { getSysDataDictList } from '@/http/dict'
import deptDialog from './components/deptDialog'
import { mapGetters } from 'vuex'
export default {
	name: 'DeptPage',
	components: { deptDialog },
	data() {
		return {
			orgNameOrInputCode: null,
			orgList: [],
			deptBelongingTypeList: [],
			whetherEnableList: [],
			deptTypeList: [],
			loading: false,
			dialogVisible: false,
			dialogTitle: '',
			rowRepetition: '',
			deptData: {
				parentUuid: '',
				orgCode: '',
				orgName: '',
				deptType: '',
				isIcuFlag: '1',
				officeType: '2',
				deptBelongingType: '',
				isCharacteristicDeptFlag: '1',
				whetherEnable: '0',
				remark: '',
			},
			deptDialogType: '',
			//输入框校验
			rules: {},
			columns: [
				{
					field: 'orgName',
					title: '医院',
				},
				{
					field: 'deptCode',
					title: '科室代码',
				},
				{
					field: 'deptName',
					title: '科室名称',
				},
				{
					field: 'deptType',
					title: '科室类别',
					slots: { default: 'deptType' },
				},
				{
					field: 'isIcuFlag',
					title: '是否ICU科室',
					slots: { default: 'isIcuFlag' },
				},
				{
					field: 'deptBelongingType',
					title: '科室所属类型',
					slots: { default: 'deptBelongingType' },
				},
				{
					field: 'isCharacteristicDeptFlag',
					title: '是否特色科室',
					slots: { default: 'isCharacteristicDeptFlag' },
				},
				{
					field: 'whetherEnable',
					title: '是否启用',
					slots: { default: 'whetherEnable' },
				},
				{
					title: '操作',
					slots: { default: 'edit' },
				},
			],
			tableData: [],
			// 按钮
			toolbarConfig: {
				slots: {
					buttons: 'toolbar_buttons',
				},
			},
			// 分页
			pageConfig: {
				currentPage: 1,
				pageSize: 10,
				pageSizes: [10, 20, 30, 50],
				total: 0,
				background: true,
				layouts: [
					'PrevPage',
					'JumpNumber',
					'NextPage',
					'FullJump',
					'Sizes',
					'Total',
				],
			},
		}
	},
	computed: {
		...mapGetters(['userMsg']),
	},
	async created() {
		this.getOrgList({
			parentUuid: this.userMsg.orgUuid,
			officeType: '2',
			whetherEnable: '0',
			delFlag: '0',
		})
		this.deptBelongingTypeList = await this.getSysDataDictList('RC_DEPT_DICT')
		this.deptTypeList = await this.getSysDataDictList('SP_DEPT_TYPE')
		this.whetherEnableList = await this.getSysDataDictList('RC_WHETHER_ENABLE')
		this.handlerSeach()
	},
	methods: {
		isFlagFun(val) {
			return val == '0' ? '是' : '否'
		},
		async getSysDataDictList(typeCode) {
			let param = { type: typeCode, delFlag: '0', whetherEnable: '0' }
			let list = []
			let res = await getSysDataDictList(param)
			if (res.data?.head?.code == 200) {
				list = res.data.data
			}
			return list
		},
		dictModelFun(dictList, cellVal) {
			let str = '-'
			if (dictList) {
				let obj = dictList.find(e => cellVal == e.value)
				if (obj) {
					str = obj.display
				}
			}
			return str
		},
		getOrgList(param) {
			getOrgList(param).then(({ data }) => {
				if (data?.head?.code == 200) {
					this.orgList = data.data
				}
			})
		},
		// 翻页事件
		pageChange({ currentPage, pageSize }) {
			this.pageConfig.currentPage = currentPage
			this.pageConfig.pageSize = pageSize
			this.handlerSeach()
		},
		// 编辑
		handlerEditRow(row) {
			this.rowRepetition = row.id
			this.dialogTitle = '科室编辑'
			this.deptDialogType = '2'
			this.deptData = Object.assign({}, row)
			this.dialogVisible = true
		},
		// 删除
		deldectDept(row) {
			this.$confirm('是否删除?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					deleteOrg(row).then(({ data }) => {
						if (data.head.code == 200) {
							this.$message({
								type: 'success',
								message: '删除成功!',
							})
							this.handlerSeach()
						}
					})
				})
				.catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除',
					})
				})
		},
		// 新增
		handlerAddClick() {
			this.dialogTitle = '科室新增'
			this.deptDialogType = '1'
			this.dialogVisible = true
		},
		// 查询
		handlerSeach() {
			let param = {
				pageNum: this.pageConfig.currentPage,
				pageSize: this.pageConfig.pageSize,
				officeType: '2',
				delFlag: '0',
				orgNameOrInputCode: this.orgNameOrInputCode
					? this.orgNameOrInputCode.toUpperCase()
					: null,
			}
			getDeptPageList(param).then(({ data }) => {
				this.tableData = data.data.records
				this.pageConfig.total = data.data.total
			})
		},
		handlerSubmit(formData, type) {
			let res = { ...formData }
			if (type == '1') {
				orgInsert(res).then(({ data }) => {
					if (data?.head?.code == 200) {
						this.$message.success('新增成功')
						this.dialogVisible = false
						this.handlerSeach()
					}
				})
			} else {
				updateOrg(res).then(({ data }) => {
					if (data?.head?.code == 200) {
						this.$message.success('编辑成功')
						this.dialogVisible = false
						this.handlerSeach()
					}
				})
			}
		},
	},
}
</script>

<style lang="scss" scoped>
.dept_input {
	width: 200px;
}
</style>
